package com.example.demo.modules.service;/**
 * Created by Administrator on 2016/11/11.
 */


import com.example.demo.modules.dao.InvoiceDao;
import com.example.demo.modules.dao.MerchantsInfoDao;
import com.example.demo.modules.dao.ShopDao;
import com.example.demo.modules.dao.ShopDateMoneyDao;
import com.example.demo.modules.entity.MerchantsInfo;
import com.example.demo.modules.entity.Shop;
import com.example.demo.modules.entity.ShopDateMoney;

import com.example.demo.util.DateUtil;
import com.example.demo.util.HttpsUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Administrator 2016/11/11
 */
@Service
public class ShopService {
    @Value("${yunnan.creditRisk.url}")
    private String yunnanCreditRiskUrl;

    @Autowired
    private MerchantsInfoDao merchantsInfoDao;

    @Autowired
    private ShopDateMoneyDao shopDateMoneyDao;

    @Autowired
    private ShopDao shopDao;

    @Autowired
    private InvoiceDao invoiceDao;

    public String creditRisk(int type, String value) {
        JSONObject object = new JSONObject();
        MerchantsInfo merchantsInfo = new MerchantsInfo();
        if("".equals(value)){
            value = " ";
        }
        if(type==0){
            merchantsInfo.setMarchantName(value);
        }else if(type==1){
            merchantsInfo.setTaxcode(value);
        }else {
            object.put("errorCode","1010");
            return object.toString();
        }
        List<MerchantsInfo> merchantsInfoList = merchantsInfoDao.findMerchant(merchantsInfo);
        System.out.println("size:"+merchantsInfoList.size());


        if(merchantsInfoList.size()>0){
            merchantsInfo = merchantsInfoList.get(0);
            Shop shop = shopDao.findById(merchantsInfo.getId());
            if(merchantsInfo.getCode().contains("TVG92")){
                //云南商户
                try {
                    String result = HttpsUtil.httpGet(yunnanCreditRiskUrl+"?shopSh="+shop.getTaxcode());
                    return result;
                } catch (Exception e) {
                    e.printStackTrace();
                    object.put("result",false);
                    object.put("errorCode","0000");
                }
            }else {
                object.put("result",shopJYLSrules(shop));  //非云南商户
            }
            object.put("errorCode","0000");
        }else {
            object.put("errorCode","1005");
        }
        return object.toString();
    }

    //商户最近三个月的单月交易流水大于3万人民币 conditionsOne true满足 false不满足
    //商户最近三个月大于每月一张开票的开票记录  conditionsTwo true满足 false不满足
    //商户最近三个月每月纳税金额最低不低于900元 conditionsThree true满足 false不满足
    public boolean shopJYLSrules(Shop shop){
        boolean conditionsOne = true;
        boolean conditionsTwo = true;
        boolean conditionsThree = true;
        String date = DateUtil.getNowDate("yyyy-MM-dd");
        String []datas = date.split("-");
        String time = datas[0]+"-"+datas[1];

        ShopDateMoney shopDateMoney = new ShopDateMoney();
        shopDateMoney.setShopno(shop.getLuckycatno());

        Map maptj = new HashMap();
        maptj.put("sid",shop.getId());
        for(int i=1;i<4;i++){
            String timedate = DateUtil.getTime(-i,1,time,"yyyy-MM");
            //商户最近三个月的单月交易流水大于3万人民币
            if(conditionsOne==true) {
                shopDateMoney.setDate(timedate);
                String mone = shopDateMoneyDao.findLSmonth(shopDateMoney);
                System.out.println(timedate+"-------->"+mone);
                double totalMoney = Double.parseDouble(mone);
                if (totalMoney < 30000) {
                    conditionsOne = false;
                    break;

                }
            }
            //商户最近三个月大于每月一张开票的开票记录
            if(conditionsTwo==true && conditionsThree==true) {
                maptj.put("date",timedate);
                Map map = invoiceDao.findByKPSE(maptj);
                if (Integer.parseInt(map.get("num").toString()) <= 1) {
                    conditionsTwo = false;
                    break;
                }
                if(Double.parseDouble(map.get("taxmoney").toString())<900){
                    conditionsThree = false;
                    break;
                }
            }
        }
        System.out.println(shop.getName()+"-"+shop.getTaxcode()+"-"+conditionsOne+"-"+conditionsTwo+"-"+conditionsThree);
        if(conditionsOne==false||conditionsTwo==false||conditionsThree==false){
            return false;
        }else {
            return true;
        }
    }

    public Shop findById(String id) {
        return shopDao.findById(id);
    }
}
